package cn.hd01.repository;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import cn.hd01.repository.entity.Product;

@Repository
public interface ProductRepository extends JpaRepository<Product, Integer> {
	
	Page<Product> findAll(Specification<Product> spec, Pageable pageable);
	
	Page<Product> findAll(Specification<Product> spec);
	
	@Modifying
	@Transactional
	@Query(value="update Product set status = ?1, update_time = now() where id=?2")
	int updateProductStatus(String status, Integer id);
	
	@Modifying
	@Transactional
	@Query(value = "update Product set name =?1, price = ?3,product_desc =?2, update_time = now() where id=?4")
	int updateProduct(String name, String desc, Double price,Integer id);
	
	@Modifying
	@Transactional
	@Query(value = "update Product set name =?1, price = ?3,product_desc =?2,parent_id=?5, update_time = now() where id=?4")
	int updateProduct(String name, String desc, Double price,Integer id,Integer parentId);
	
	/**
	 * 查询所有二级产品列表
	 * @return
	 */
	@Query(value="select id,name,productDesc,price,parentId,createTime from  Product where parent_id is not null")
	List<Product> findProductList();
	
	/**
	 * 查询所有父类产品
	 * @return
	 */
	@Query(value="select id,name,productDesc,price,parentId,createTime from  Product where parent_id is null")
	List<Product> findParentProductList();
	
	
}
